<template>
	<view class="form_page">
		<view class="head">
			<image src="/static/image/back.png" @click="handleBack" />
			<view class="title_text">{{inspectForm.projectPartName}}</view>
			<view class="blank"></view>
		</view>

		<view class="utils">
			<view class="util" @click="exportInspect">
				<image src="/static/image/export2.png" mode=""></image> 导出报验申请表
			</view>
			<view class="util" @click="submitInspect">
				<image src="/static/image/submit.png" mode=""></image> 提交报验
			</view>
			<view class="util end" v-if="!isEdit" @click="handleEdit(true)">
				<image src="/static/image/edit2.png" mode=""></image> 编辑
			</view>
			<view class="util end editing" v-else>
				<image src="/static/image/edit3.png" mode=""></image> 编辑中
			</view>
		</view>

		<scroll-view scroll-y class="scroll">
			<form @submit="formSubmit" @reset="formReset">
				<view class="form_item">
					<view class="title_text">工程部位名称</view>
					<view class="my-input">
						<input v-model="inspectForm.projectPartName" :disabled="!isEdit" class="uni-input"
							name="projectPartName" />
					</view>
				</view>
				<view class="form_item">
					<view class="title_text">该部位设计情况</view>
					<view class="my-textarea">
						<textarea v-model="inspectForm.designStatus" :disabled="!isEdit" name="designStatus" />
					</view>
				</view>
				<view class="form_item">
					<view class="title_text">该部位自检情况</view>
					<view class="my-textarea">
						<textarea v-model="inspectForm.inspectionStatus" :disabled="!isEdit" name="inspectionStatus" />
					</view>
				</view>
				<view class="form_item">
					<view class="title_text">项目监理机构</view>
					<view class="my-input">
						<input v-model="inspectForm.supervisionAgency" :disabled="!isEdit" class="uni-input"
							name="supervisionAgency" />
					</view>
				</view>
				<view class="form_item">
					<view class="title_text">监理</view>
					<view class="my-input">
						<input v-model="inspectForm.supervisorName" :disabled="!isEdit" class="uni-input"
							name="supervisorName" />
					</view>
				</view>
				<view class="form_item">
					<view class="title_text">监理确认是否合格</view>
					<!-- 					<view class="my-input">
						<input v-model="inspectForm.isQualified" :disabled="!isEdit" class="uni-input"
							name="isQualified" />
					</view> -->
					<uni-data-checkbox :disabled="!isEdit" v-model="inspectForm.isQualified"
						:localdata="isQualifiedRange" @change="changeQualified"></uni-data-checkbox>
				</view>
				<view class="form_item">
					<view class="title_text">自检资料</view>
					<view class="my-input">
						<input v-model="inspectForm.selfChecking" :disabled="!isEdit" class="uni-input"
							name="selfChecking" />
					</view>
				</view>
				<view class="form_item">
					<view class="title_text">质量检查工程师</view>
					<view class="my-input">
						<input v-model="inspectForm.qaEngineer" :disabled="!isEdit" class="uni-input"
							name="qaEngineer" />
					</view>
				</view>
				<view class="form_item">
					<view class="title_text">问题及整改要求</view>
					<view class="my-textarea">
						<textarea v-model="inspectForm.issuesAndRequirements" :disabled="!isEdit"
							name="issuesAndRequirements" />
					</view>
				</view>

				<view class="form_item">
					<view class="title_text">是否旁站</view>
					<uni-data-checkbox :disabled="!isEdit" v-model="inspectForm.isOnSite" :localdata="isOnSiteRange"
						@change="changeOnSite"></uni-data-checkbox>
				</view>
				<view class="form_item">
					<view class="title_text">旁站人员</view>
					<view class="my-input">
						<input v-model="inspectForm.siteName" :disabled="!isEdit" class="uni-input" name="siteName" />
					</view>
				</view>
				<view class="form_item">
					<view class="title_text">下道工序施工情况</view>
					<view class="my-textarea">
						<textarea v-model="inspectForm.nextProcessPreparation" :disabled="!isEdit"
							name="nextProcessPreparation" />
					</view>
				</view>
				<view class="btn" v-if="isEdit">
					<button class="cancel" type="primary" @click="handleEdit(false)">退出</button>
					<button form-type="submit" class="confirm" type="primary">保存</button>
				</view>
			</form>
		</scroll-view>

		<u-popup :show="showExport" mode='center' @close="handleClose">
			<view class="my-popup">
				<view class="head" @click="handleClose">
					<image src="/static/image/back.png" mode="" />
					<view class="title_text">请选择导出类型</view>
					<view class="blank"></view>
				</view>
				<view class="form">
					<form @submit="exportSubmit" ref="form">
						<uni-data-checkbox class="exportType" v-model="exportType"
							:localdata="exportRange"></uni-data-checkbox>
						<button form-type="submit" class="confirm" type="primary">确定</button>
					</form>
				</view>
			</view>
			<!-- <ExportPopup :showExport="showExport"></ExportPopup> -->
		</u-popup>
		
	</view>
</template>

<script>
	import request from '@/utils/request.js'
	import ExportPopup from '@/components/ExportPopup.vue'
	
	export default {
		components: {
			ExportPopup
		},
		data() {
			return {
				isOnSiteRange: [{
					"value": true,
					"text": "是"
				}, {
					"value": false,
					"text": "否"
				}],
				isQualifiedRange: [{
					"value": "合格",
					"text": "合格"
				}, {
					"value": "不合格",
					"text": "不合格"
				}],
				exportRange: [{
					"value": 0,
					"text": "word"
				}, {
					"value": 1,
					"text": "pdf"
				}],
				id: '',
				isEdit: false,
				showExport: false,
				exportType: 0,
				inspectForm: {
					id: '',
					projectPartName: '',
					designStatus: '',
					inspectionStatus: '',
					supervisionAgency: '',
					supervisorName: '',
					isQualified: '',
					selfChecking: '',
					qaEngineer: '',
					issuesAndRequirements: '',
					isOnSite: '',
					siteName: '',
					nextProcessPreparation: '',
				}
			}
		},
		onLoad(option) {
			console.log(option, 'onLoad');
			this.id = option.id
			console.log(this.id, 'this.id')
			this.initInspect()
		},
		methods: {
			initInspect() {
				request(`/supervision/ledger/info?id=${this.id}`).then((res) => {
					if (res.data.code == 200) {
						console.log(res.data, 'initInspect');
						for (let key in this.inspectForm) {
							// if (res.data.data[key]) {
							// }
							this.inspectForm[key] = res.data.data[key]
						}
						console.log(this.inspectForm, 'this.inspectForm');
					}
				})
			},
			handleClose() {
				this.showExport = false
			},
			changeOnSite(e) {
				console.log(e, 'changeOnSite');
			},
			changeQualified(e) {
				console.log(e, 'changeQualified');
			},
			exportInspect() {
				this.showExport = true
			},
			submitInspect() {
				request(`/supervision/ledger/submit?id=${this.id}`, {
					method: 'POST'
				}).then((res) => {
					if (res.data.code == 200) {
						console.log(res.data, 'initInspect');
					}
				})
			},
			chooseOnSite(param) {
				console.log(param.detail.value);
				this.inspectForm.isOnSite = param.detail.value
			},
			handleBack() {
				uni.navigateBack({
					delta: 1
				})
			},
			handleEdit(isEdit) {
				this.isEdit = isEdit
			},
			exportSubmit() {
				let that = this
				request(`/supervision/ledger/export`, {
					method: 'POST',
					data: {
						type: this.exportType,
						ids: [this.id]
					}
				}).then((res) => {
					if (res.data.code == 200) {
						let exportUrl = res.data.data[0]
						console.log(exportUrl, 'exportSubmit');
						uni.downloadFile({
							url: exportUrl, // 文件链接
							success: function(res) {
								const tempFilePath = res.tempFilePath; // 获取临时文件路径
								// 在这里可以处理文件的下载或保存操作
								console.log(tempFilePath,'tempFilePath');
								uni.getFileSystemManager().saveFile({
									tempFilePath: tempFilePath, // 上一步获取的临时文件路径
									success: function(res) {
										const savedFilePath = res.savedFilePath; // 获取保存后的文件路径
										// 在这里可以处理保存后的文件访问操作
										console.log(savedFilePath,'savedFilePath');
										that.showExport = false
										uni.showToast({
											icon: 'none',
											title: '保存成功',
											duration: 2000
										});
									},
									fail: function(error) {
										// 处理保存失败的情况
										console.log(error,'error');
									}
								});
							},
							fail: function(error) {
								// 处理下载失败的情况
							}
						});
					}
				})
			},
			formSubmit(e) {
				const inspect = e.detail.value
				console.log(this.inspectForm, 'formSubmit');
				request(`/supervision/ledger/update`, {
					method: 'POST',
					data: this.inspectForm
				}).then((res) => {
					if (res.data.code == 200) {
						uni.showToast({
							icon: 'none',
							title: '编辑成功',
							duration: 2000
						});
						this.isEdit = false
					}
					console.log(res.data, 'updateDirectory');
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "@/style/formPage.scss";
</style>